<template>
  <div class="upload-box">
    <h1 class="title1">导入书签</h1>
    <el-upload
      ref="uploadRef"
      action="#"
      :on-change="onSuccess"
      :on-exceed="handleExceed"
      :limit="1"
      :auto-upload="false"
      drag
      accept=".html"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">
        将文件拖到此处或<em>点击上传</em>
        <div class="tip">只能上传.html格式的文件</div>
      </div>
    </el-upload>
    <div>
      <el-button
        type="primary"
        @click="upload"
        style="width: 100%; margin-top: 10px"
        >导入书签</el-button
      >
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, markRaw, nextTick } from "vue";
import { UploadFilled } from "@element-plus/icons-vue";
import { genFileId } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { mAxios, encodeURL } from "~/utils/axios/axios.js";
const userId = Number(sessionStorage.getItem('user_id'))
// 上传本地文件
const uploadRef = ref();
const handleExceed = (files) => {
  uploadRef.value.clearFiles();
  const file = files[0];
  file.uid = genFileId();
  uploadRef.value.handleStart(file);
};
const filerow = ref(null);
const onSuccess = (file) => {
  if (file.status === "ready") {
    filerow.value = file.raw;
  }
};
const upload = () => {
  const formData = new FormData();
  formData.append("userId", userId)
  formData.append("file", filerow.value);
  mAxios
    .post("upload/bookmark", formData, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    })
    .then((res) => {
      res.data.succeed
        ? ElMessage.success("导入成功")
        : ElMessage.error(res.data.message);
      uploadRef.value.clearFiles();
      filerow.value = null
    });
};
</script>

<style lang="scss" scoped>
.upload-box {
  width: 50%;
}
.tip {
  margin-top: 5px;
  color: red;
}
.title1 {
  margin-bottom: 25px;
  font-size: 17px;
  font-weight: 500;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji",
    "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
</style>
